﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimioAPI;
using SimioAPI.Extensions;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;


namespace WLC
{
    class WLCStoreIntermediateProductStepDefinition : IStepDefinition
    {
        #region IStepDefinition Members

        public IStep CreateStep(IPropertyReaders properties)
        {
            return new WLCStoreIntermediateProductStep(properties);
        }

        public void DefineSchema(IPropertyDefinitions schema)
        {
            IPropertyDefinition itemName = schema.AddStringProperty("ItemName", "");
            itemName.Description = "The product name to store (Set to 'order' if the name of the order object should be used).";
            itemName.Required = true;
            
            IPropertyDefinition amount = schema.AddExpressionProperty("Amount", string.Empty);
            amount.Description = "The amount to store.";
            amount.Required = true;

            IPropertyDefinition orderID = schema.AddExpressionProperty("OrderID", string.Empty);
            orderID.Description = "The order to reserve this product for (-1 for freely available products, -2 if value should be taken from store.).";
            orderID.Required = true;

            IPropertyDefinition intermediateProdStore = schema.AddElementProperty("IntermediateProductStore", WLCIntermediateProductStoreElementDefinition.ID);
            intermediateProdStore.Description = "The product store to store in.";
            intermediateProdStore.Required = true;

            IPropertyDefinition orderSequence = schema.AddElementProperty("OrderSequence", WLCSequenceElementDefinition.ID);
            intermediateProdStore.Description = "The order sequence to store the type for which this order is reserved.";
            intermediateProdStore.Required = true;
        }

        public int NumberOfExits
        {
            get { return 1; }
        }

        public string Name
        {
            get { return "StoreIntermediateProduct"; }
        }

        public string Description
        {
            get { return ""; }
        }

        public System.Drawing.Image Icon
        {
            get { return null; }
        }

       
        public Guid UniqueID
        {
            get { return new Guid("{3951C464-1A79-11DF-9493-FD8A55D89593}"); }
        }

        #endregion
    }


    class WLCStoreIntermediateProductStep : IStep
    {


        Hashtable _reservations;

        IPropertyReader _itemNameProp;
        IPropertyReader _amountProp;
        IPropertyReader _orderIDProp;
        IElementProperty _productStoreProp;
        IElementProperty _orderSequenceProp;

        public WLCStoreIntermediateProductStep(IPropertyReaders bag)
        {
            _itemNameProp = bag.GetProperty("ItemName");
            _amountProp = bag.GetProperty("Amount");
            _orderIDProp = bag.GetProperty("OrderID");
            _productStoreProp = (IElementProperty)bag.GetProperty("IntermediateProductStore");
            _orderSequenceProp = (IElementProperty)bag.GetProperty("OrderSequence");
            
            

        }

        #region IStep Members

        private string GetOrderType(IStepExecutionContext context)
        {
            string rawType = context.AssociatedObject.ExecutionContext.ToString();
            string[] splitType = rawType.Split(' ');


            return splitType[1];
        }

        public ExitType Execute(IStepExecutionContext context)
        {
            
            
            string itemName = _itemNameProp.GetStringValue(context);
            
            if(itemName == "order") 
                itemName = GetOrderType(context);
            

            // Get the amount to store from the property
            double amount = (double)_amountProp.GetDoubleValue(context);

            WLCIntermediateProductStoreElement productStore = (WLCIntermediateProductStoreElement)_productStoreProp.GetElement(context);

            // Get the reservations hash table from the product store
            _reservations = productStore.Reservations;

            // Save the order for which this order is reserved with the sequence
            WLCSequenceElement orderSequence = (WLCSequenceElement)_orderSequenceProp.GetElement(context);

            int orderIDNr = (int)_orderIDProp.GetDoubleValue(context);
            string orderID = "";

            if (orderIDNr == -1)
                orderID = "Free";
            else if (orderIDNr == -2)
                // Get the order ID to reserve this product for
                orderID = productStore.NextOrderReservedFor;
            else
            {
                orderID = orderSequence.ReservedFor;
            }

            if (orderID == null || context.AssociatedObject.States["IsCopy"].StateValue == 1)
                orderID = (string)_reservations[orderIDNr];

            context.ExecutionInformation.TraceInformation("STORE: Trying to store " + amount + " units of " + itemName + " for order with ID " + orderID);
            productStore.Store(orderID, itemName, amount);


            
            orderSequence.ReservedFor = orderID;
            _reservations[orderIDNr] = orderID;
              
                       
            

            

               

            Debug.Flush(); 
            return ExitType.FirstExit;
        }

        #endregion

    }


}
